<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="doAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" :index="Nindex" width="120" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="doEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="doDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :page-size="pageParams.pagesize"
                layout="prev,pager,next"
                :total="total"
                :current-page="pageParams.page"
                @current-change="hCurChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>

      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :title="isEdit?'编辑角色':'新增角色'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      :is-edit="isEdit"
    >
      <el-form ref="roleForm" :model="formData" label-width="100px">
        <el-form-item label="角色名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small">取消</el-button>
          <el-button size="small" type="primary" @click="doSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getRoles, delRoles, addRoles, editRoles } from '@/api/settings'
export default {
  // 1.数据
  data() {
    return {
      // 1.1角色的数据
      list: [],
      // 1.2分页的数据
      pageParams: {
        page: 1,
        pagesize: 10
      },
      // 1.3 总条数
      total: 0,
      // 1.4表单数据
      formData: {
        name: '',
        description: ''
      },
      // 1.5弹框的布尔值
      showDialog: false,
      // 1.6判断新增编辑的布尔值
      isEdit: false
    }
  },
  // 2.created
  created() {
    // 2.1加载角色列表
    this.loadRoles()
  },
  // 3.方法
  methods: {
    // 3.1获取角色列表
    async loadRoles() {
      const res = await getRoles(this.pageParams)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 3.2删除
    async doDel(id) {
      await delRoles(id)
      // 如果是最后一条数据,则页码-1
      if (this.list.length === 1) {
        let page = this.pageParams.page
        page--
        page = Math.max(1, page)
        this.pageParams.page = page
      }
      this.$message.success('删除成功')
      this.loadRoles()
    },
    // 3.3当页码变化时
    hCurChange(page) {
      // (1)修改页码
      this.pageParams.page = page
      // (2)重新加载
      this.loadRoles()
    },
    // 3.4点击添加角色
    doAdd() {
      this.showDialog = true
      this.isEdit = false
      this.formData = {
        name: '',
        description: ''
      }
    },
    // 3.5点击新增的确认
    async hAdd() {
      await addRoles(this.formData)
      // 弹窗提示+关闭弹窗+刷新数据
      this.$message.success('添加成功')
      this.showDialog = false
      this.total++
      this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
      this.loadRoles()
    },
    // 3.6点击确认按钮
    doSubmit() {
      this.isEdit ? this.hEdit() : this.hAdd()
    },
    // 3.7点击编辑按钮
    async doEdit(row) {
      this.showDialog = true
      this.isEdit = true
      this.formData = { ...row }
    },
    // 3.8点击编辑的确认
    async hEdit() {
      await editRoles(this.formData)
      this.showDialog = false
      this.loadRoles()
      this.$message.success('编辑成功')
    },
    // 3.8解决分页后序号改变的问题
    Nindex(index) {
      // 当前页数 - 1 * 每页数据条数 + 1
      const page = this.pageParams.page // 当前页码
      const pagesize = this.pageParams.pagesize // 每页条数
      return index + 1 + (page - 1) * pagesize
    }
  }
}
</script>
